<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$cat_id = MiscUtils::getParam('cat', 0);

$SIZE = 20;
$page = MiscUtils::getParam('pg', 1);
$order = MiscUtils::getParam('o', 't.id');
$queue = MiscUtils::getParam('q', 'DESC');

$categories = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_category', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

$root = new stdClass();
$root->id = 0;
$root->name = 'ROOT';
$root->parent_id = -1;
__create_root($categories, $root);

$rslt = new stdClass();
$rslt->data = array();
__get_all($root, $cat_id, $rslt, 0);
  
$cond_vals = NULL;
if ($cat_id != 0) {
  $cond_vals = new stdClass(); 
  $cond_vals->c = '';
  $cond_vals->v = array();
  foreach ($rslt->data as $idx => &$d) {
    if ($cond_vals->c != '') {
      $cond_vals->c .= ' OR ';
    }
    $cond_vals->c .= "t.category_id = :v" . ($idx + 1);
    $cond_vals->v[":v" . ($idx + 1)] = "$d";
  }
}
$opt = new stdClass();
$opt->select_expr = 'id, track_id, date, enabled_stock, enabled_delivery, enabled_member_price, brand_id, category_id, name, price, stock, stock_alert, intro, status, alert__';
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, $order, $queue, $page, $SIZE, $opt);
foreach ($items->d as $item) {
  $item->_img = 'images/no_image.gif';
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.product_track_id = :v1 AND is_default = 0';
  $cond_vals->v = array(':v1' => $item->track_id);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_img', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    if (file_exists($tmp[0]->file)) {
      $item->_img = $tmp[0]->file;
    }
    
  }
  
  $item->_prc = $item->price;
  if (isset($_SESSION['index' . USER]) && $_SESSION['index' . USER]->id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.product_track_id = :v1 AND t.member_id = :v2';
    $cond_vals->v = array(':v1' => $item->track_id, ':v2' => $_SESSION['index' . USER]->member_id);
    $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_member_price', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) > 0) {
      $item->_prc = $tmp[0]->price;
    }
  }
}

function __get_all($node, $id, $rslt, $is_to_add) {
  if ($node->id == $id || $is_to_add == 1) {
    $rslt->data[] = $node->id;
  }
  foreach ($node->c as &$child) {
    __get_all($child, $id, $rslt, (($is_to_add === 1) ? $is_to_add : $node->id == $id));
  }
}
function __create_root($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->c = array();
      $node->c[] = $tmp;
      __create_root($arr, $tmp);
    }
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 12px;
  width: 100%;
}
table.list td {
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
  padding: 0 5px;
  height: 25px;
  cursor: pointer;
  font: normal 13px Lucida Sans Unicode;
}
table.list tr.rowodd td {
  background-color: #eee;
}
table.list tr.roweven td {
  background-color: #fff;
}
table.list tr.rowodd:hover td {
  background-color: #ccc;
}
table.list tr.roweven:hover td {
  background-color: #ccc;
}
table.list tr.none td {
  background-color: #fff;
}
table.list tr.current td {
  color: #fff;
  background-color: #666;
}
.prdt {
  border: 1px solid #ddd;
  padding: 5px 10px;
  text-align: center;
  margin-left: 6px;
  margin-bottom: 10px;
  width: 140px;
  height: 140px;
  display: inline-block;
  *display: inline;
  zoom: 1;
}
.prdt img {
  width: 120px;
  height: 96px;
  cursor: pointer;
}
.none {
  font: bold 15px Lucida Sans Unicode;
}
</style>
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/jquery.simplePagination.js"></script>
<link type="text/css" rel="stylesheet" href="css/simplePagination.css"/>
<script>
$(function() {
  <?php if ($items->t > $items->s) { ?>
  $('#pagination').pagination({
    items: <?php echo $items->t; ?>,
    itemsOnPage: <?php echo $items->s; ?>,
    currentPage: <?php echo $items->p; ?>,
    cssStyle: 'compact-theme',
    onPageClick: function(pageNumber, event) {
      parent.location.href = 'http://<?php echo URL . '?i=' . $page_id . '&pg='; ?>' + pageNumber;
    }
  });
  <?php } ?>
});
</script>
</head>
<body>
<table cellpadding=0 cellspacing=0>
  <tr>
    <td style="text-align: left;vertical-align: top;width: 210px;">
      <table id="pages" class="list" cellpadding=0 cellspacing=0>
        <tr class="rowodd <?php if ($cat_id == 0) echo 'current'; ?>">
          <td onclick="parent.location.href = 'http://<?php echo URL . '?i=' . $page_id; ?>';" style="font-weight: bold;">All Categories</td>
        </tr>
      </table>
    </td>
    <td style="width: 10px;">&nbsp;</td>
    <td style="text-align: left;vertical-align: top;width: 730px;">
      <?php foreach ($items->d as $item) { ?>
      <div class="prdt">
        <div><b><?php echo MiscUtils::getShortName($item->name, 10); ?></b></div>
        <img src="<?php echo $item->_img; ?>" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&id=' . $item->track_id; ?>')"></img>
        <div>$<?php echo $item->_prc; ?></div>
      </div>
      <?php } ?>
      <?php if (count($items->d) == 0) { ?>
      <div class="none">No Porducts Found</div>
      <?php } ?>
      <?php if ($items->t > $items->s) { ?>
      <div>&nbsp;</div>
      <div id="pagination"></div>
      <?php } ?>
      <div>&nbsp;</div>
    </td>
  </tr>
</table>
<script type="text/javascript" src="../../../libs/json/json2.js"></script>
<script type="text/javascript" src="../../../incls/js/pageutils.js"></script>
<script type="text/javascript">
var _root;
$(function(){
  _root = PageUtils.getRoot(JSON.parse('<?php echo addslashes(json_encode($categories)); ?>'));
  setRoot('', false);
});

function setRoot(prefix, is_child) {
  $('#pages').find('tr:gt(0)').remove();
  _setRoot(_root, $('#pages')[0], prefix, is_child);
}

function _setRoot(node, table, prefix, is_child) {
  var tr, td, elm;
  if (node.id != 0) {
    tr = table.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'rowodd' : 'roweven';
    if (node.id == '<?php echo $cat_id?>') {
      tr.className = ' current';
    }
    
    td = tr.insertCell(-1);
    td._node = node;
    td.appendChild(document.createTextNode(prefix + ((node.id != 0 && node.name == '') ? '' : node.name)));
    td.onclick = function() {
      parent.location.href = 'http://<?php echo URL . '?i=' . $page_id . '&cat='; ?>' + this._node.id;
    };
    
  } else if (node.id == 0 && node._children.length == 0) {
    tr = table.insertRow(-1);
    tr.className = 'none';
    td = tr.insertCell(-1);
    td.colSpan = 4;
    td.appendChild(document.createTextNode('None'));
  }
  for (var idx in node._children) {
    var obj = node._children[idx];
    _setRoot(obj, table, prefix + '\u2002\u2002', is_child)
  }
}
</script>
</body>
</html>